
<template>
  <div class="department" id="department">
    <!-- 右侧 -->
    <div class="department_right">
      <!-- 机构名称 -->
      <div class="mechanism_nav app-container-search">
        <!-- 左侧 -->
        <el-form ref="form" :model="getList" label-width="110px">
          <el-row :gutter="20">
            <el-col :span="6"
              ><el-form-item label="项目名称：">
                <el-select
                  class="selectBhz"
                  v-model="getList.fprojectid"
                  placeholder="所有项目"
                  @change="selectProject"
                >
                  <el-option
                    v-for="v in projectsObj"
                    :key="v.fItemName"
                    :label="v.fItemName"
                    :value="v.fItemValue"
                  >
                  </el-option>
                </el-select> </el-form-item
            ></el-col>
            <el-col :span="6"
              ><el-form-item label="标段名称：">
                <el-select
                  class="selectBhz"
                  v-model="getList.fCompanyid"
                  placeholder="所有标段"
                  @change="selectTender"
                >
                  <el-option
                    v-for="v in tendersObj"
                    :key="v.fItemName"
                    :label="v.fItemName"
                    :value="v.fItemValue"
                  >
                  </el-option>
                </el-select> </el-form-item
            ></el-col>
            <el-col
              :span="12"
              class="searchBtn"
              style="text-align: right; display: flex; justify-content: end"
            >
              <el-form-item>
                <el-button
                  type="primary"
                  :loading="editLoading"
                  @click="yj_searchForm(getList)"
                  >查询</el-button
                >
                <el-button @click="reset">重置</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <!-- 顶部导航栏 -->
      <div v-if="!pageFlag" class="networkStatus">
        <div
          class="networkStatusBigBox"
          v-for="(item, index) in tableData"
          :key="index"
        >
          <div class="networkImg">
            <img :src="item.STATUS == 0 ? online2 : online1" alt="" />
          </div>
          <div class="networkInfo">
            <div class="bhClass bhjname">
              {{ item.F_StationName }}-{{ item.F_MachineName }}
            </div>
            <div class="bhClass bhjid">拌合机ID:{{ item.F_MachineId }}</div>
            <div class="bhClass bhjid">拌合站ID:{{ item.F_StationId }}</div>
            <div class="bhClass bhjid">
              上传时间:{{ item.F_DischargeDates }}
            </div>
          </div>
          <div class="btn">
            <!-- 0:停用 1：启用 -->
            <el-switch
              :value="item.F_ActiveStatus | status"
              :active-text="
                item.F_ActiveStatus == 0
                  ? '停用'
                  : item.F_ActiveStatus == 1
                  ? '启用'
                  : '未启用'
              "
            >
            </el-switch>
          </div>
        </div>
      </div>
      <div v-if="pageFlag" class="pageFlag">
        <img src="@/assets/img/undata.png" alt="" />
        <div class="pageFlagText">暂时没有数据哦！</div>
      </div>
    </div>
  </div>
</template>

<script>
import actions from "@/actions";

import tree from "@/components/public/el-trees"; //左侧机构树
import elBreadcrumb from "@/components/public/el-breadcrumb.vue"; //面包屑
import publicForm from "@/components/public/el-form.vue";
import noData from "@/components/public/noData.vue";
import {
  getMachineList, // 新增按钮拌合机状态
  getMachineStatus, // 拌合机在线状态
  addMachineActive, // 保存按钮拌合机状态
  updateMachineActive, // 修改 启用停用/开始上传日期
  getMachineActive, // 获取 拌合机状态详情
} from "@/api/request.js";
export default {
  name: "networkStatus",
  data() {
    return {
      name: "networkStatus", //按钮配置页面代号
      rowInfo: null, //当前行信息
      projectsObj: [], //项目名
      tendersObj: [],
      editLoading: false,

      highlightBd: true, //左侧机构树停留背景色
      online2: require("@/assets/img/tong/icon_sw_online2.png"),
      online1: require("@/assets/img/tong/icon_sw_online1.png"),
      treelength: false, //左侧树显示隐藏
      checked: true, //查看下级按钮
      showName: "未选择公司",
      loadingDialog: false, //拌合机绑定loading
      dialogVisible: false, //拌合机绑定dialog
      loadingMachineIdDialog: false, //拌合机详情loading
      dialogMachineIdVisible: false, //拌合机详情dialog
      machineList: [], //拌合机列表
      checkMachine: [],
      selectMachineList: [], //选择绑定拌和机数组
      formData: {
        fCompanyid: "", // 施工标段id
        fMachineid: "", // 拌合机编号/id
        fDischargedates: "", // 开始上传时间
        fActivestatus: "", // 上传启用停用 1-启用 0-停用
        userName: "", // 当前登陆人名称
        userId: "", // 当前登陆人id
      }, //部门 新增编辑向后台传递对象
      formObj: [
        //新增编辑配置参数
        {
          input: true, //是input
          label: "拌合机编号",
          prop: "fMachineid",
          width: 24, //参考el-col
          disabled: true,
        },
        {
          dateTime: true,
          label: "上传时间",
          prop: "fDischargedates",
          placeholder: "请选择",
          width: 12, //参考el-col
          disabled: false,
          rules: [
            {
              required: true,
              message: "请选择上传时间",
            },
          ], //验证
        },

        {
          //新增编辑配置参数
          radio: true,
          label: "是否启用",
          prop: "fActivestatus",
          placeholder: "请选择",
          width: 24, //参考el-col
          disabled: false,
          options: [
            {
              label: "启用",
              value: "1",
            },
            {
              label: "停用",
              value: "0",
            },
          ],
        },
      ],
      fId: null, //点击当前列表数据fId
      listLoading: false, //列表数据加载loading
      // 列表给后端传参
      getList: {
        endDates: "",
        fCheckStatus: "",
        fCloseoperation: "",
        fCompanyid: "",
        fprojectid: "",
        machineId: "",
        modelType: "",
        page: 1,
        pageSize: 15,
        startDates: "",
        stationId: "",
        strengthgrade: "",
        userId: "",
        warningLevel: "",
      },
      pageFlag: true,
      tableData: [], //拌合站数据
      userInfo: {}, //登录用户
    };
  },
  components: {
    tree, //左侧组织机构数
    elBreadcrumb, //面包屑
    publicForm,
    noData,
  },
  filters: {
    status(data) {
      var status;
      if (data == 1) {
        status = true;
      } else if (data == 0) {
        status = false;
      }
      return status;
    },
  },
  mounted() {
    actions.onGlobalStateChange((state, prev) => {
      this.token = prev.token;
      this.userInfo = prev.currentUser;
      var data = prev.currentUser.projects;
      this.projectsObj = [
        {
          fItemName: "所有项目",
          fItemValue: "",
        },
      ];
      data.map((item) => {
        this.projectsObj.push({
          fItemName: item.projectName,
          fItemValue: item.projectId,
        });
      });
    }, true);

    if (JSON.parse(sessionStorage.getItem("GxEmbedded_getList"))) {
      var getListStorage = JSON.parse(
        sessionStorage.getItem("GxEmbedded_getList")
      );
      this.getList = getListStorage;
      if (getListStorage.fprojectid != "") {
        this.getTender(getListStorage.fprojectid, 0);
      }
    } else {
      this.getList.fprojectid = this.projectsObj[1].fItemValue;
      this.getTender(this.getList.fprojectid, 0);
    }
  },

  methods: {
    selectProject(e) {
      this.tendersObj = [];
      this.bhzDataList = [];
      this.getList.fCompanyid = "";
      this.getList.stationId = "";
      this.getList.machineId = "";
      this.bhjDataList = [];
      this.getTender(e, 1);
    },
    async getTender(e, num) {
      await this.$http(
        " http://172.18.5.31:8300/IOTManagement/Base/Company/getGXJLBaseCompanyList.do?projectId=" +
          e,
        {
          headers: {
            "Content-Type": "application/json",
            Authorization: localStorage.getItem("access_token"),
          },
        }
      ).then((res) => {
        if (res.status == 200) {
          var data = res.data.data.GXCompanyList;
          this.tendersObj = [];
          if (data.length == 1) {
            data.map((item) => {
              this.tendersObj.push({
                fItemName: item.sectionName,
                fItemValue: item.sectionId,
              });
            });
            this.getList.fCompanyid = data[0].sectionId;
          } else {
            this.tendersObj = [
              {
                fItemName: "所有标段",
                fItemValue: "",
              },
            ];
            data.map((item) => {
              this.tendersObj.push({
                fItemName: item.sectionName,
                fItemValue: item.sectionId,
              });
            });
          }

          if (num == 0) {
            // num==0 调列表；num==1 不调
            this.yj_searchForm(this.getList);
          }
        }
      });
    },
    selectTender(e) {
      this.bhjDataList = [];
      this.bhzDataList = [];
    },
    selectStation(e) {
      this.bhjDataList = [];
    },
    // 重置按钮
    reset() {
      this.timeDuring = "";
      this.getList = {
        endDates: "",
        fCheckStatus: "",
        fCloseoperation: "",
        fCompanyid: "",
        fprojectid: "",
        machineId: "",
        modelType: "",
        page: 1,
        pageSize: 15,
        startDates: "",
        stationId: "",
        strengthgrade: "",
        userId: "",
        warningLevel: "",
      };
    },

    // 获取列表数据
    yj_searchForm(data) {
      this.listLoading = true;
      window.sessionStorage.setItem("GxEmbedded_getList", JSON.stringify(data));
      var id;
      if (data.fCompanyid != "") {
        id = data.fCompanyid;
      } else {
        id = data.fprojectid;
      }

      var sc = {
        fCompanyid: id, // 施工标段id
      };
      // fprojectid: "",
      //   // companyId: "", // 施工标段id
      //   companyId: "", // 施工标段id
      //   startDates: "", // 开始时间
      //   endDates: "", // 截至时间
      //   machineId: "", // 拌和机编号
      //   enCode: "",
      getMachineStatus(sc)
        .then((res) => {
          if (res.errcode == 0) {
            var that = this;
            that.listLoading = false;
            if (res.data.length > 0) {
              this.tableData = res.data;
              this.pageFlag = false;
            } else {
              this.pageFlag = true;
              console.log("暂无数据");
            }
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    // 绑定拌和机保存接口
    yj_saveObjectForm(num) {
      if (num == 0) {
        //绑定拌合机
        if (this.selectMachineList.length > 0) {
          addMachineActive({ machineIds: this.selectMachineList }).then(
            (res) => {
              if (res.errcode == 0) {
                this.dialogVisible = false;
                this.yj_searchForm(this.getList);
                this.$message.success("绑定成功");
              } else {
                console.log("绑定拌和机保存接口报错");
              }
            }
          );
        } else {
          this.$message.warning("请选择需要绑定的拌和机");
        }
      } else if (num == 1) {
        this.formData.fCompanyid = this.getList.companyId;
        if (this.$refs.ObjectForm.submitForm()) {
          console.log(this.formData);

          updateMachineActive(this.formData)
            .then((res) => {
              if (res.errcode == 0) {
                this.yj_searchForm(this.getList);
                this.dialogMachineIdVisible = false;
                this.$message.success("修改成功");
              } else {
                this.$message.error(res.msg);
                console.log("接口报错");
              }
            })
            .catch((error) => {
              console.log("拌合站设置-新增）", error);
            });
        } else {
          console.log("error");
        }
      }
    },
    // 新增编辑
    yj_editForm(num, machineId) {
      console.log(this.userInfo.userId);
      if (this.userInfo.userId == "System") {
        if (num == 0) {
          // 打开绑定拌和机弹框
          this.loadingDialog = true;
          this.dialogVisible = true;

          getMachineList({ fCompanyid: this.getList.companyId }).then((res) => {
            if (res.errcode == 0) {
              this.loadingDialog = false;
              this.machineList = res.data;
            } else {
              console.log("获取拌和机列表接口报错");
            }
          });
        } else if (num == 1) {
          this.$router.push({
            path: "/hntManage/network-status-detail",
          });

          getMachineActive({ machineId: machineId }).then((res) => {
            if (res.errcode == 0) {
              this.loadingMachineIdDialog = false;
              this.formData = res.data;
            } else {
              console.log("获取拌和机列表接口报错");
            }
          });
        }
      }
    },
    // 点击拌和机得到拌和机machineId数组
    selectMachine(e) {
      this.selectMachineList = e;
    },
  },
};
</script>

<style scoped lang="less">
.networkStatus {
  // background: pink;
  flex: 1;
  // padding: 10px;
  display: flex;
  flex-wrap: wrap;
  box-sizing: border-box;
  min-width: 740px;
  align-content: flex-start;

  .networkStatusBigBox {
    background-color: #fafafa;
    border: 1px solid #ccc;
    height: 32.6%;
    height: 35%;

    // height: 100rpx;
    // 每个元素都要设置右边距margin-right（每个元素的左右间隙）
    // 同时设置下边距margin-bottom（每个元素的上下间隙）
    margin: 0 10px 10px 0;
    width: calc((100% - 40px) / 5);
    // 这里一行显示4个，所以是/4，一行显示几个就除以几
    // 这里的60rpx = (分布个数4-1)*间隙20rpx, 可以根据实际的分布个数和间隙区调整
    min-width: calc((100% - 40px) / 5);
    max-width: calc((100% - 40px) / 5);
    border-radius: 10px;
    box-sizing: border-box;
    padding: 10px 15px;
    display: flex;
    flex-direction: column;

    .networkImg {
      // height: 150px;
      height: 45%;
      // background: pink;
      display: flex;
      justify-content: center;
      align-items: center;

      img {
        width: 100%;
        min-width: 30%;
        min-height: 80%;
        max-height: 90%;
        // width: 30%;
        // min-width: 30%;
        // min-height: 60%;
        // max-height: 90%;
      }
    }

    .networkInfo {
      height: 40%;
      border-bottom: 1px solid #eee;
      // background: red;
      font-size: 12px;
      padding: 0;
      display: flex;
      flex-direction: column;

      .bhClass {
        display: flex;
        // justify-content: center;
        align-items: center;
        height: 25%;
        overflow: hidden; //（文字长度超出限定宽度，则隐藏超出的内容）
        white-space: nowrap; //（设置文字在一行显示，不能换行）
        text-overflow: ellipsis; //（规定当文本溢出时，显示省略符号来代表被修剪的文本）
      }

      .bhjname {
        color: #171a1d;
        font-weight: 700;
        font-size: 16px;
      }

      .bhjid {
        color: #858687;

        font-size: 14px;
        // line-height: 1;
        // height: 0.23rem;
        height: 25%;
      }
    }

    .btn {
      height: 15%;
      font-size: 14px;
      display: flex;
      align-items: center;
    }

    // 每行最右侧的那个不设置右外边距
    &:nth-child(5n + 5) {
      margin-right: 0;
    }

    &:hover {
      /* 盒子向上移动 */
      transform: translateY(-1px);
      /* 背景 */
      box-shadow: 0 20px 80px rgba(48, 49, 51, 0.15);
    }
  }
}

.addMemberCheckLeft {
  width: 98%;
  padding: 0 0 0 20px;
  // border-top: 1px solid rgba(221, 221, 221, 1);
  // border-bottom: 1px solid rgba(221, 221, 221, 1);
  // border-right: 1px solid rgba(221, 221, 221, 1);

  .addMemberCheck {
    width: 233px;
    height: 64px;
    border-radius: 2px;
    display: flex;
    align-items: center;
    position: absolute;
    top: 0;
    left: 0;
  }
}

.addMemberCheck .MemberIcon {
  /* height: 100%; */
  /* height: 54px; */
  height: 62px;
  width: 64px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 28px;
  color: #fff;
  // margin-right: 8px;
  background: rgba(224, 224, 224, 1);
}

// .MemberIcon img {
//     width: 37.81px;
//     height: 39.38px;
//     border-radius: 50%;
// }

.MemberDetailInfos {
  position: relative;
  flex: 1;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  overflow: hidden;

  .name {
    color: rgba(51, 51, 51, 1);
    font-size: 12px;
    height: 20px;
    line-height: 20px;
  }

  .hostName {
    color: rgba(128, 125, 125, 1);
    font-size: 12px;
    height: 20px;
    line-height: 20px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

.MemberIcon .name {
  color: rgba(51, 51, 51, 1);
  font-size: 12px;
  height: 20px;

  line-height: 20px;
}

.MemberIcon .hostName {
  color: rgba(128, 125, 125, 1);
  font-size: 12px;
  height: 20px;
  line-height: 20px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pageFlag {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;

  justify-content: center;
  align-items: center;
  color: #171a1d;
  font-size: 16px;
  // background: pink;

  img {
    width: 258px;
    height: 208px;
  }

  .pageFlagText {
    margin-top: 33px;
  }

  .pageFlagTexts {
    font-size: 14px;
    color: #a2a4a7;
  }
}

.addMachine /deep/.el-checkbox-button {
  width: 235px;
  height: 64px;
}

#smallDialog /deep/.el-dialog {
  width: 700px !important;
}
</style>
